package com.zh.auth.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zh.auth.domain.dto.*;
import com.zh.auth.service.RolePermissionService;
import com.zh.auth.service.RoleService;
import com.zh.common.annotation.NeedAuth;
import com.zh.common.annotation.NeedPermission;
import com.zh.common.annotation.NeedRoles;
import com.zh.common.annotation.Operator;
import com.zh.common.constant.AuthConstant;
import com.zh.web.base.ApiResult;
import com.zh.web.base.BaseController;
import com.zh.web.base.PageApiResult;
import com.zh.web.domain.auth.vo.RoleVo;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@Api(tags = "角色管理")
@Operator
@Validated
public class RoleController extends BaseController {
    private final RoleService roleService;
    private final RolePermissionService rolePermissionService;

    @ApiOperationSupport(order = 1)
    @PostMapping("/page")
    @ApiOperation(value = "分页查询角色", notes = "角色携带权限信息")
    public ApiResult<PageApiResult<RoleVo>> pageRole(@RequestBody PageRoleDto pageRoleDto) {
        return buildSuccessPackage(roleService.pageRole (pageRoleDto));
    }

    @ApiOperationSupport(order = 2)
    @GetMapping("/{code}")
    @ApiOperation(value = "code查询角色", notes = "角色携带权限信息")
    public ApiResult<RoleVo> getRoleById(@PathVariable Integer code) {
        return buildSuccessPackage(roleService.getByCode(code));
    }

    @ApiOperationSupport(order = 3)
    @PostMapping("/add")
    @ApiOperation(value = "新增角色信息,需要管理员或者insert")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.INSERT)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> addRole(@RequestBody AddRoleDto addRoleDto) {
        addRoleDto.setCreateOperator(operator);
        addRoleDto.setUpdateOperator(operator);
        return buildMsgPackage(roleService.addRole(addRoleDto));
    }

    @ApiOperationSupport(order = 4)
    @PutMapping("/update")
    @ApiOperation(value = "修改角色信息,需要管理员或者update")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.UPDATE)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    @GlobalTransactional
    public ApiResult<String> updateRole(@RequestBody UpdateRoleDto updateRoleDto) {
        updateRoleDto.setUpdateOperator(operator);
        return buildMsgPackage(roleService.updateRole(updateRoleDto));
    }

    @ApiOperationSupport(order = 5)
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除角色信息,需要管理员或者delete")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.DELETE)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> deleteRole(@RequestBody List<Long> ids) {
        return buildMsgPackage(roleService.deleteRole(ids));
    }

    @ApiOperationSupport(order = 6)
    @GetMapping("/getByUserId")
    @ApiOperation(value = "根据用户ID查询角色集合")
    public ApiResult<List<RoleVo>> getRoleByUserId(Long id) {
        return buildSuccessPackage(roleService.getRoleByUserId(id));
    }

    @ApiOperationSupport(order = 7)
    @GetMapping("/getRoleName2Code")
    @ApiOperation(value = "获取角色名称-编码对应关系",hidden = true)
    public ApiResult<Map<String, Integer>> getRoleName2Code() {
        return buildSuccessPackage(roleService.getRoleName2Code());
    }

    @ApiOperationSupport(order = 7)
    @PostMapping("/permission/add")
    @ApiOperation(value = "添加权限到角色,需要管理员或者insert")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.INSERT)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> addPermission2Role(@Valid @RequestBody AddPermission2RoleDto addPermission2RoleDto) {
        addPermission2RoleDto.setCreateOperator(operator);
        addPermission2RoleDto.setUpdateOperator(operator);
        return buildMsgPackage(rolePermissionService.addPermission2Role(addPermission2RoleDto));
    }

    @ApiOperationSupport(order = 8)
    @DeleteMapping("/permission/delete")
    @ApiOperation(value = "删除角色的权限,需要管理员或者delete")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.DELETE)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> delPermission2Role(@Valid @RequestBody DelPermission2RoleDto delPermission2RoleDto) {
        delPermission2RoleDto.setCreateOperator(operator);
        delPermission2RoleDto.setUpdateOperator(operator);
        return buildMsgPackage(rolePermissionService.delPermission2Role(delPermission2RoleDto));
    }
}
